// 
// $Rev:: 27            $:  Revision of last commit
// $Author:: rvhaverm@g#$:  Author of last commit
// $Date:: 2010-07-26 1#$:  Date of last commit
// 
package agents.hosts;

import com.ibm.aglet.AgletContext;
import com.ibm.aglet.AgletProxy;
import com.ibm.aglet.system.AgletRuntime;
import com.ibm.aglet.system.ContextListener;

import agents.agents.AgentFactory;
import agents.aglets.AgletBridge;
import agents.configuration.IHostConfiguration;
import agents.exceptions.IllegalCodeBaseException; //import agents.exceptions.IllegalUserInterfaceException;
import agents.interfaces.IAgentHost;
import agents.interfaces.IHostEventListener;
import agents.interfaces.ILogger;
import agents.messages.MessageFactory; //import agents.messages.PrintMessage;
import agents.messages.SetSystemPropertiesMessage;


public class BaseAgentHost implements IAgentHost//TODO: ContextListener
{
    /**
     * This is the runtime-environment in which our Aglet-framework executes.
     */
    protected AgletRuntime _runtime = null;

    /**
     * Serves as a home for the agents
     */
    protected AgletContext _context = null;

    private AgletProxy _receptionist = null;

    private ILogger _logger = null;

    // /**
    // * A representative for the agent that is responsible for the User
    // * Interface.
    // */
    // private AgletProxy _uiAgent = null;

    private IHostConfiguration _configuration = null;

    public BaseAgentHost(IHostConfiguration props, AgletRuntime runtime,
	    AgletContext context, ILogger logger)
    {
	_runtime = runtime;
	_context = context;
	_configuration = props;
	_logger = logger;
    }

    @Override
    public void addHostListener(IHostEventListener listener)
    {
	_context.addContextListener(listener);
    }

    @Override
    public void start()
    {
	_context.start();

	activateReceptionist();
    }

    @Override
    public IHostConfiguration getConfiguration()
    {
	return _configuration;
    }

    /**
     * Activate a receptionist agent.
     * 
     * This method creates a Receptionist agent within the AgletContext,
     * context_, and sends it the HostProperties, properties_, through a
     * SetSystemPropertiesMessage
     * 
     * @see agents.agents.Receptionist
     * @see agents.server.HostProperties
     * @see agents.msgs.SetSystemPropertiesMessage
     */
    private void activateReceptionist()
    {
	try
	{
	    _receptionist = AgentFactory.newReceptionist(_context, null);
	    SetSystemPropertiesMessage setProps = MessageFactory
		    .CreateSetSystemPropertiesMessage(_configuration);

	    AgletBridge.sendMessage(_receptionist, setProps.getMessage());

	}
	catch (IllegalCodeBaseException e)
	{
	    _logger.log("ERROR intialising the Receptionist. Arriving agents "
		    + "will not be able to function normally! " + e);
	}
	catch (Exception e)
	{
	    _logger.log("ERROR intialising the Receptionist. Arriving agents "
		    + "will not be able to function normally! " + e);
	}
    }

    @Override
    public AgletProxy getReceptionist()
    {
	return _receptionist;
    }
}
